import com.adobe.serialization.json.*;
import com.jinher.lib.App;
import com.jinher.lib.Const.AppConst;
import com.jinher.lib.Const.LnConst;
import com.jinher.lib.Module.Login;
import com.jinher.lib.Net;

import flash.display.NativeWindowSystemChrome;
import flash.display.NativeWindowType;


//系统托盘图标
[Embed(source='assets/icons/icon_16.png')]
private var icon16:Class;
[Embed(source='assets/icons/icon_32.png')]
private var icon32:Class;
[Embed(source='assets/icons/icon_48.png')]
private var icon48:Class;
[Embed(source='assets/icons/icon_128.png')]
private var icon128:Class;

private var net:Net=new Net();
private var app:App=new App();

//创建系统托盘
private function createTray():void
{
	var trayMenu:NativeMenu=createTrayMenu();
	var icon:InteractiveIcon=NativeApplication.nativeApplication.icon;
	if (NativeApplication.supportsDockIcon)
	{
		(icon as DockIcon).menu=trayMenu;
	}
	else
	{
		(icon as SystemTrayIcon).menu=trayMenu;
		(icon as SystemTrayIcon).tooltip=LnConst.Name;
	}
	var bitmaps:Array=[new icon16(), new icon32(), new icon48(), new icon128()];
	icon.bitmaps=bitmaps;
}

//系统托盘菜单
private function createTrayMenu():NativeMenu
{
	var menu:NativeMenu=new NativeMenu();
	var quitCmd:NativeMenuItem=new NativeMenuItem(LnConst.Exist);
	quitCmd.addEventListener(Event.SELECT, quitHandler);
	menu.addItem(quitCmd);
	return menu;
}

//退出系统
private function quitHandler(evt:Event):void
{
	NativeApplication.nativeApplication.exit();
}

//开始网络监听，监听间隔为3秒
private function startMonitor():void
{
	var url:String=AppConst.MonitorUrl;
	net.addEventListener(Net.Event_Net_StatusChange, statusHandler);
	net.checkNet(url);
}

//网络状态发生改变时
private function statusHandler(evt:Event):void
{
	trace("Available:" + Net.urlMonitor.available);
}

private function initiApp():void
{
	//开始网络监听
	startMonitor();

	//创建系统托盘
	createTray();
	
	//登录窗口
	showLogin();
}

private function showLogin():void
{
	//获取登录窗口信息
	var loginInfo:Login=AppConst.LoginInfo;
	var loginWin:LoginWin=new LoginWin();
	loginWin.width=loginInfo.Width;
	loginWin.height=loginInfo.Height;
	loginWin.title=LnConst.Login;
	//无框，透明
	loginWin.systemChrome=NativeWindowSystemChrome.NONE;
	loginWin.transparent=true;
	//不显示在状态栏
	loginWin.type=NativeWindowType.LIGHTWEIGHT;
	//禁用改变窗口大小
	loginWin.resizable=false;
	//是否始终在最前
	loginWin.alwaysInFront=app.getConfig().AlwaysInFront;
	loginWin.open(true);
	this.visible=false;
}

private function PostTest(event:Event):void
{
/*
net.addEventListener(Net.Event_Net_PostBack, onPostBackSucceed);
net.addEventListener(Net.Event_Net_PostError, onPostBackError);
var url:String="http://oa.jh0101.com/jhc6/JHSoft.Wcf/POSTServiceForApp.svc/Authorize";
var rdata:Object=new Object();
rdata.sign="012062$50D113106D357AAA59118D333DF7688E9232FC7C";

net.getJSON(url, JSON.encode(rdata));
*/




}

private function onPostBackSucceed(e:Event):void
{
	net.removeEventListener(Net.Event_Net_PostBack, onPostBackSucceed);
	net.removeEventListener(Net.Event_Net_PostError, onPostBackError);
	var data:Object=net.data;
}

private function onPostBackError(e:Event):void
{
	net.removeEventListener(Net.Event_Net_PostBack, onPostBackSucceed);
	net.removeEventListener(Net.Event_Net_PostError, onPostBackError);
	var data:Object=net.data;
}
